﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace JQMergeFramework
{
    public class CardItem : MonoBehaviour
    {
        public int mIndex;
        
        public Image buildingImg;
        public GameObject lockIcon;
        
        private Transform buildingImgTrans;
        
        private Scrollbar mBar = null;
        private float x;
        
        private CityTargetCfg mCityCfg;

        private float mDes;
        public CityTargetCfg CityCfg
        {
            get { return mCityCfg; }
        }

        private Vector3 imgScale;
        
        private void Awake()
        {
            buildingImgTrans = buildingImg.transform;
        }


        public void BindCityCfg(int indexs,CityTargetCfg cfg, Scrollbar bar,float dis,float distance)
        {
            x = distance * indexs;
            mIndex = indexs;
            mCityCfg = cfg;
            mBar = bar;
            mDes = dis;
            buildingImg.sprite = MergeManager.Instance.scriptablePrefab.GetBuildingSprite(cfg.imgName);

            bool isLock = cfg.lv <= DataManager.Instance.AchievementFinishIndex;
            buildingImg.color = isLock ? Color.white : Color.black;
            lockIcon.SetActive(!isLock);
            
            buildingImg.SetNativeSize();
            this.gameObject.SetActive(true);
        }

        

       public void UpdatePos()
         {
             if (mBar!=null)
             {
                 transform.localPosition = new Vector3(-(mBar.value - x) * mDes, 0, 0);
                 float scale = 1 - Mathf.Abs(mBar.value - x) * 10f;
                 if (scale < 0.5f)
                 {
                     scale = 0.5f;
                 }
                 if (imgScale!=buildingImgTrans.localScale )
                 {
                     buildingImgTrans.localScale = new Vector3(scale, scale, 1);
                 }
             }
         } 
    }
}